package com.rock.code.bigdata.bigdatainterface.controller;

import com.rock.code.bigdata.bigdatainterface.service.CreateTabService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @author zhouhao
 * @description 创建历史表接口
 * @date 2018/9/12 11:33
 * @Version 1.0
 */
@Controller
@RequestMapping("/createHisTable")
public class CreateHistoryTableController {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
    private Logger logger = Logger.getLogger(CreateHistoryTableController.class);

    private static final String exec_tabname_day = "TB_KPI_VALUE_DAY_";
    private static final String exec_tabname_month = "TB_KPI_VALUE_MONTH_";


    @Resource
    private CreateTabService createTabService;


    /*
    建历史表接口  天表
     */
    @ResponseBody
    @RequestMapping(value = "/saveHisTabDay", method = RequestMethod.GET)
    public String saveHisTabDay(@RequestParam("startday") String startday, @RequestParam("endday") String endday) throws Exception {

        String date = startday;
        String tabName = null;
        while (sdf.parse(date).before(sdf.parse(endday))) {

            tabName = exec_tabname_day + date.replaceAll("-", "");
            if (createTabService.isExists(tabName)) {
                createTabService.createTabHis(tabName);
            } else {
                logger.info(tabName + "已创建");
            }
            date = dateAddDay(date);

        }
        return "建表成功";


    }


    /*
    建历史表接口  月表
     */
    @ResponseBody
    @RequestMapping(value = "/saveHisTabMonth", method = RequestMethod.GET)
    public String saveHisTabMonth(@RequestParam("startMonth") String startMonth, @RequestParam("endMonth") String endMonth) throws Exception {

        String date = startMonth;
        String tabName = null;
        while (sdfMonth.parse(date).before(sdfMonth.parse(endMonth))) {

            tabName = exec_tabname_month + date.replaceAll("-", "");
            if (createTabService.isExists(tabName)) {
                createTabService.createTabHis(tabName);
            } else {
                logger.info(tabName + "已创建");
            }
            date = subMonth(date);

        }
        return "建表成功";


    }

    /****
     * 传入具体日期 ，返回具体日期增加一个月。
     * @param date 日期(2017-04-13)
     * @return 2017-05-13
     * @throws ParseException
     */
    public static String subMonth(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, 1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        System.out.println(reStr);
        return reStr;

    }


    /**
     * @return 输入日期的后一天
     * @Author zh
     * @Description //日期加一天
     * @Param 日期
     **/
    public static String dateAddDay(String beginTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date bt = sdf.parse(beginTime);
        Calendar c = Calendar.getInstance();
        c.setTime(bt);
        c.add(Calendar.DAY_OF_MONTH, 1);//日期加一天
        Date tomorrow = c.getTime();
        System.out.println(sdf.format(tomorrow));
        return sdf.format(tomorrow);
    }


}
